<template>
  <div class="login-page">
    <!-- 背景装饰 -->
    <div class="background-decoration">
      <div class="circle circle-1"></div>
      <div class="circle circle-2"></div>
      <div class="circle circle-3"></div>
    </div>
    
    <!-- 主要内容 -->
    <div class="login-container">
      <!-- 左侧信息面板 -->
      <div class="info-panel">
        <div class="brand-section">
          <div class="brand-icon">
            <el-icon size="48"><User /></el-icon>
          </div>
          <h1 class="brand-title">暖星心里守护站</h1>
          <p class="brand-subtitle">安全便捷的身份验证</p>
        </div>
        
        <div class="features-section">
          <div class="feature-item">
            <el-icon class="feature-icon"><Lock /></el-icon>
            <div class="feature-content">
              <h3>安全认证</h3>
              <p>采用先进的人脸识别技术，确保您的身份安全可靠</p>
            </div>
          </div>
          <div class="feature-item">
            <el-icon class="feature-icon"><Lightning /></el-icon>
            <div class="feature-content">
              <h3>快速登录</h3>
              <p>一键人脸识别，快速进入您的专属心理健康空间</p>
            </div>
          </div>
          <div class="feature-item">
            <el-icon class="feature-icon"><User /></el-icon>
            <div class="feature-content">
              <h3>专属服务</h3>
              <p>为您提供个性化的心理健康管理和专业治疗建议</p>
            </div>
          </div>
        </div>
        
        <div class="bottom-info">
          <p>没有账户？<router-link to="/register" class="link">立即注册</router-link></p>
          <p>医生用户？<router-link to="/doctor/login" class="link">密码登录</router-link></p>
        </div>
      </div>
      
      <!-- 右侧登录面板 -->
      <div class="login-panel">
        <div class="login-content">
          <!-- 登录表单 -->
          <div class="login-form">
            <div v-if="!loginResult" class="camera-section">
              <!-- 登录标题 -->
              <div class="login-header">
                <h2 class="login-title">登录</h2>
                <p class="login-subtitle">请对准摄像头，让我们为您提供专业的心理健康服务</p>
              </div>
              <!-- 人脸拍摄组件 -->
              <div class="camera-wrapper">
                <CameraCapture
                  @photo-confirmed="handlePhotoConfirmed"
                  @retake-photo="handleRetakePhoto"
                  :width="400"
                  :height="300"
                  :show-external-buttons="true"
                  :reset="!hasPhoto"
                  class="camera-component"
                />
              </div>
              
              <!-- 按钮区域 -->
              <div class="action-section" v-if="hasPhoto">
                <div class="button-row">
                  <el-button
                    type="primary"
                    size="large"
                    @click="handleLogin"
                    :loading="logging"
                    class="action-btn login-btn"
                  >
                    <el-icon v-if="!logging"><Unlock /></el-icon>
                    {{ logging ? '识别中...' : '开始登录' }}
                  </el-button>
                  <el-button
                    type="primary"
                    size="large"
                    @click="handleRetakePhoto"
                    class="action-btn retake-btn"
                  >
                    <el-icon><RefreshRight /></el-icon>
                    重新拍摄
                  </el-button>
                </div>
              </div>
              
            </div>
            
            <!-- 登录结果 -->
            <div v-else class="result-section">
              <div v-if="loginResult.success" class="login-success-container">
                <!-- 成功图标 -->
                <div class="success-icon-wrapper">
                  <div class="success-icon">
                    <el-icon size="60"><CircleCheck /></el-icon>
                  </div>
                </div>
                
                <!-- 成功标题 -->
                <div class="success-header">
                  <h2 class="success-title">登录成功</h2>
                </div>
                
                <!-- 用户信息卡片 -->
                <div v-if="loginResult.data" class="modern-user-card">
                  <div class="user-info-section">
                    <div class="face-id-display">
                      <span class="face-id-label">人脸ID</span>
                      <div class="face-id-value">{{ loginResult.data.faceId }}</div>
                    </div>
                    
                    <div class="stats-grid">
                      <div class="stat-card">
                        <div class="stat-icon-wrapper">
                          <el-icon><DataAnalysis /></el-icon>
                        </div>
                        <div class="stat-content">
                          <span class="stat-label">相似度</span>
                          <span class="stat-value">{{ (loginResult.data.similarity * 100).toFixed(2) }}%</span>
                        </div>
                      </div>
                      
                      <div class="stat-card">
                        <div class="stat-icon-wrapper">
                          <el-icon><Clock /></el-icon>
                        </div>
                        <div class="stat-content">
                          <span class="stat-label">登录时间</span>
                          <span class="stat-value">{{ formatLoginTime(loginResult.data.loginTime) }}</span>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
                
                <!-- 操作按钮 -->
                <div class="success-actions">
                  <el-button type="primary" @click="resetLogin" size="large" class="modern-button">
                    <el-icon><RefreshRight /></el-icon>
                    重新登录
                  </el-button>
                  <el-button @click="goToHome" size="large" class="modern-button secondary">
                    <el-icon><HomeFilled /></el-icon>
                    进入系统
                  </el-button>
                </div>
              </div>
              
              <!-- 登录失败 -->
              <div v-else class="login-error-container">
                <el-result
                  icon="error"
                  title="登录失败"
                  :sub-title="loginResult.message"
                >
                  <template #extra>
                    <div class="error-actions">
                      <el-button type="primary" @click="resetLogin" size="large">
                        重新登录
                      </el-button>
                    </div>
                  </template>
                </el-result>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

import { User, Lock, Lightning, Unlock, RefreshRight, CircleCheck, DataAnalysis, Clock, HomeFilled } from '@element-plus/icons-vue'
import CameraCapture from '../../components/CameraCapture.vue'
import { faceLogin } from '../../api/face.js'

const router = useRouter()
const hasPhoto = ref(false)
const photoFile = ref(null)
const logging = ref(false)
const loginResult = ref(null)

const handlePhotoConfirmed = (blob) => {
  photoFile.value = blob
  hasPhoto.value = true
}

const handleRetakePhoto = () => {
  hasPhoto.value = false
  photoFile.value = null
  // 通知CameraCapture组件重新拍摄
}

const handleLogin = async () => {
  if (!photoFile.value) {
    return
  }
  
  logging.value = true
  
  try {
    const formData = new FormData()
    formData.append('faceImage', photoFile.value, 'face.jpg')
    
    const response = await faceLogin(formData)
    
    loginResult.value = {
      success: true,
      message: response.msg || '登录成功',
      data: response.data
    }
    
  } catch (error) {
    console.error('Login error:', error)
    
    loginResult.value = {
      success: false,
      message: error.message || '登录失败，请重试'
    }
  } finally {
    logging.value = false
  }
}

const resetLogin = () => {
  loginResult.value = null
  hasPhoto.value = false
  photoFile.value = null
}

const goToHome = () => {
  // 跳转到首页
  router.push('/home')
}

const formatLoginTime = (timeStr) => {
  if (!timeStr) return ''
  
  try {
    const date = new Date(timeStr)
    const now = new Date()
    
    // 如果是今天
    if (date.toDateString() === now.toDateString()) {
      return date.toLocaleTimeString('zh-CN', { 
        hour: '2-digit', 
        minute: '2-digit' 
      })
    } else {
      return date.toLocaleDateString('zh-CN', { 
        month: '2-digit', 
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit'
      })
    }
  } catch (error) {
    return timeStr
  }
}
</script>

<style scoped>
.login-page {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: 
    /* 主渐变背景 - 温暖治愈色调 */
    linear-gradient(135deg, 
      rgba(165, 214, 167, 0.9) 0%,   /* 轻柔绿色 */
      rgba(129, 199, 132, 0.8) 25%,  /* 治愈绿色 */
      rgba(79, 195, 247, 0.8) 75%,   /* 宁静蓝色 */
      rgba(144, 202, 249, 0.9) 100%  /* 淡雅蓝色 */
    ),
    /* 自然叶子图案 */
    url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 80 80"><defs><pattern id="leaves" width="80" height="80" patternUnits="userSpaceOnUse"><g opacity="0.1"><path d="M20,40 Q25,20 40,25 Q55,30 50,45 Q45,60 30,55 Q15,50 20,40" fill="white"/><path d="M60,20 Q65,0 80,5 Q95,10 90,25 Q85,40 70,35 Q55,30 60,20" fill="white"/><circle cx="10" cy="70" r="3" fill="white" opacity="0.3"/><circle cx="70" cy="60" r="2" fill="white" opacity="0.2"/></g></pattern></defs><rect width="100%" height="100%" fill="url(%23leaves)"/></svg>'),
    /* 治愈光圈效果 */
    radial-gradient(circle at 20% 30%, rgba(255,255,255,0.15) 0%, transparent 40%),
    radial-gradient(circle at 80% 70%, rgba(129, 199, 132,0.2) 0%, transparent 50%),
    radial-gradient(circle at 60% 20%, rgba(79, 195, 247,0.15) 0%, transparent 45%),
    /* 底层基础渐变 */
    linear-gradient(45deg, #e8f5e8 0%, #e3f2fd 100%);
  background-size: 
    100% 100%,           /* 主渐变 */
    80px 80px,           /* 叶子图案 */
    300px 300px,         /* 光圈1 */
    400px 400px,         /* 光圈2 */
    350px 350px,         /* 光圈3 */
    100% 100%;           /* 底层渐变 */
  background-repeat: no-repeat;
  background-position: center center;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow-x: auto;
  overflow-y: auto;
  z-index: 0;
}

.background-decoration {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 1;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: linear-gradient(135deg, 
    rgba(255, 255, 255, 0.15) 0%, 
    rgba(129, 199, 132, 0.1) 50%, 
    rgba(79, 195, 247, 0.1) 100%
  );
  backdrop-filter: blur(25px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  animation: healingFloat 8s ease-in-out infinite;
  box-shadow: 
    0 8px 32px rgba(129, 199, 132, 0.15),
    inset 0 1px 0 rgba(255, 255, 255, 0.3);
}

.circle-1 {
  width: 180px;
  height: 180px;
  top: 8%;
  left: 8%;
  animation-delay: 0s;
}

.circle-2 {
  width: 120px;
  height: 120px;
  top: 55%;
  right: 12%;
  animation-delay: 2.5s;
}

.circle-3 {
  width: 90px;
  height: 90px;
  bottom: 15%;
  left: 18%;
  animation-delay: 5s;
}

/* 添加额外的治愈元素 */
.circle::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 60%;
  height: 60%;
  border-radius: 50%;
  background: radial-gradient(circle, 
    rgba(255, 255, 255, 0.3) 0%, 
    transparent 70%
  );
  transform: translate(-50%, -50%);
}

@keyframes healingFloat {
  0%, 100% { 
    transform: translateY(0px) rotate(0deg) scale(1); 
    opacity: 0.6;
  }
  33% { 
    transform: translateY(-15px) rotate(120deg) scale(1.05); 
    opacity: 0.8;
  }
  66% { 
    transform: translateY(-8px) rotate(240deg) scale(0.95); 
    opacity: 0.7;
  }
}

.login-container {
  position: relative;
  z-index: 2;
  display: flex;
  width: min(1200px, 95vw);
  min-width: 300px;
  height: min(700px, 90vh);
  min-height: 500px;
  background: 
    linear-gradient(135deg, rgba(255, 255, 255, 0.98) 0%, rgba(248, 252, 248, 0.95) 100%);
  border-radius: 24px;
  box-shadow: 
    0 25px 50px rgba(0, 0, 0, 0.12),
    0 0 0 1px rgba(129, 199, 132, 0.1),
    inset 0 1px 0 rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(25px);
  overflow: hidden;
  margin: 20px;
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.info-panel {
  flex: 1;
  background: linear-gradient(135deg, #81C784 0%, #4FC3F7 100%);
  color: white;
  padding: 60px 50px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  position: relative;
}

.info-panel::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grid" width="10" height="10" patternUnits="userSpaceOnUse"><path d="M 10 0 L 0 0 0 10" fill="none" stroke="rgba(255,255,255,0.1)" stroke-width="0.5"/></pattern></defs><rect width="100" height="100" fill="url(%23grid)"/></svg>');
  opacity: 0.3;
}

.brand-section {
  position: relative;
  z-index: 1;
  text-align: center;
}

.brand-icon {
  margin-bottom: 20px;
}

.brand-title {
  font-size: 32px;
  font-weight: 700;
  margin: 0 0 10px 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.brand-subtitle {
  font-size: 18px;
  opacity: 0.9;
  margin: 0;
}

.features-section {
  position: relative;
  z-index: 1;
}

.feature-item {
  display: flex;
  align-items: flex-start;
  margin-bottom: 30px;
}

.feature-icon {
  margin-right: 20px;
  margin-top: 5px;
  font-size: 24px;
  opacity: 0.9;
}

.feature-content h3 {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 8px 0;
}

.feature-content p {
  font-size: 14px;
  opacity: 0.9;
  margin: 0;
  line-height: 1.5;
}

.bottom-info {
  position: relative;
  z-index: 1;
  text-align: center;
  font-size: 16px;
  opacity: 0.9;
}

.link {
  color: white;
  text-decoration: none;
  font-weight: 600;
  border-bottom: 1px solid rgba(255, 255, 255, 0.5);
  transition: all 0.3s ease;
}

.link:hover {
  border-bottom-color: white;
}

.login-panel {
  flex: 1;
  padding: 60px 50px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-content {
  width: 100%;
  max-width: 480px;
}

.login-header {
  text-align: center;
  margin-top: 30px;
  margin-bottom: 40px;
}

.login-title {
  font-size: 28px;
  font-weight: 700;
  color: #333;
  margin: 0 0 10px 0;
}

.login-subtitle {
  color: #666;
  font-size: 16px;
  margin: 0;
}

.camera-section {
  text-align: center;
}

.camera-wrapper {
  margin-top: 25px;
  margin-bottom: 30px;
  padding-top: 20px;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.camera-component {
  border-radius: 16px;
}

.action-section {
  margin-bottom: 30px;
}

.button-row {
  display: flex;
  gap: 15px;
  justify-content: center;
}

.action-btn {
  flex: 1;
  height: 50px;
  border-radius: 25px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, #81C784 0%, #4FC3F7 100%);
  border: none;
  box-shadow: 0 4px 15px rgba(129, 199, 132, 0.4);
  transition: all 0.3s ease;
  max-width: 180px;
}

.action-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(129, 199, 132, 0.6);
}


/* 登录成功页面样式 */
.login-success-container {
  text-align: center;
  padding: 15px 20px 20px 20px;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.95) 0%, rgba(248, 252, 248, 0.92) 100%);
  border-radius: 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  margin-top: 30px;
}

.success-icon-wrapper {
  margin-bottom: 20px;
}

.success-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 100px;
  height: 100px;
  background: linear-gradient(135deg, #4CAF50 0%, #81C784 100%);
  border-radius: 50%;
  color: white;
  margin: 0 auto;
  box-shadow: 0 8px 24px rgba(76, 175, 80, 0.3);
  animation: successPulse 2s ease-in-out infinite;
}

@keyframes successPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.05); }
}

.success-header {
  margin-bottom: 20px;
}

.success-title {
  font-size: 32px;
  font-weight: 700;
  color: #2E7D32;
  margin: 0 0 10px 0;
  background: linear-gradient(135deg, #2E7D32 0%, #4CAF50 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.success-subtitle {
  font-size: 16px;
  color: #666;
  margin: -15px 0 0 0;
  opacity: 0.8;
}

/* 现代化用户卡片 */
.modern-user-card {
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.9) 0%, rgba(248, 252, 248, 0.8) 100%);
  border-radius: 20px;
  padding: 30px;
  margin: 30px 0;
  box-shadow: 
    0 8px 32px rgba(0, 0, 0, 0.08),
    inset 0 1px 0 rgba(255, 255, 255, 0.5);
  border: 1px solid rgba(255, 255, 255, 0.3);
  backdrop-filter: blur(10px);
}


.user-info-section {
  text-align: left;
}

.face-id-display {
  background: linear-gradient(135deg, rgba(76, 175, 80, 0.1) 0%, rgba(129, 199, 132, 0.1) 100%);
  border-radius: 16px;
  padding: 20px;
  margin-bottom: 20px;
  border: 1px solid rgba(76, 175, 80, 0.2);
  text-align: center;
}

.face-id-label {
  display: block;
  font-size: 14px;
  color: #4CAF50;
  font-weight: 600;
  margin-bottom: 8px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.face-id-value {
  font-size: 18px;
  font-weight: 700;
  color: #2E7D32;
  font-family: 'Courier New', monospace;
  word-break: break-all;
  line-height: 1.4;
  background: rgba(46, 125, 50, 0.1);
  padding: 10px;
  border-radius: 8px;
}

.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 15px;
  margin-bottom: 30px;
}

.stat-card {
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.8) 0%, rgba(248, 252, 248, 0.6) 100%);
  border-radius: 16px;
  padding: 20px;
  display: flex;
  align-items: center;
  gap: 15px;
  border: 1px solid rgba(76, 175, 80, 0.1);
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.stat-icon-wrapper {
  width: 48px;
  height: 48px;
  background: linear-gradient(135deg, #4CAF50 0%, #66BB6A 100%);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 20px;
  box-shadow: 0 4px 12px rgba(76, 175, 80, 0.3);
}

.stat-content {
  flex: 1;
  text-align: left;
}

.stat-label {
  display: block;
  font-size: 12px;
  color: #666;
  font-weight: 500;
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.stat-value {
  font-size: 16px;
  color: #2E7D32;
  font-weight: 700;
}

/* 操作按钮样式 */
.success-actions {
  display: flex;
  gap: 15px;
  justify-content: center;
  margin-top: 30px;
}

.modern-button {
  border-radius: 25px;
  padding: 12px 30px;
  font-weight: 600;
  font-size: 16px;
  min-width: 140px;
  transition: all 0.3s ease;
  border: none;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.modern-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

.modern-button.secondary {
  background: linear-gradient(135deg, #E8F5E8 0%, #C8E6C9 100%);
  color: #2E7D32;
  border: 1px solid rgba(76, 175, 80, 0.3);
}

.modern-button.secondary:hover {
  background: linear-gradient(135deg, #C8E6C9 0%, #A5D6A7 100%);
}

/* 登录错误页面 */
.login-error-container {
  padding: 20px;
}

.error-actions {
  display: flex;
  justify-content: center;
  gap: 15px;
}


/* 响应式设计 */
@media (max-width: 1280px) {
  .login-container {
    width: min(1000px, 95vw);
    height: min(600px, 90vh);
  }
  
  .info-panel, .login-panel {
    padding: 40px 35px;
  }
  
  .brand-title {
    font-size: 28px;
  }
}

/* 超小屏幕和极端缩放优化 */
@media (max-width: 480px), (max-height: 480px) {
  .login-page {
    padding: 10px;
  }
  
  .login-container {
    width: 100%;
    min-width: 280px;
    height: auto;
    min-height: 400px;
    margin: 10px;
    border-radius: 16px;
  }
  
  .info-panel, .login-panel {
    padding: 20px 15px;
  }
  
  .brand-title {
    font-size: 22px;
  }
  
  .login-title {
    font-size: 20px;
  }
  
  .camera-wrapper {
    margin-top: 15px;
    margin-bottom: 15px;
    padding-top: 15px;
  }
  
  .features-section {
    display: none;
  }
}

@media (max-width: 1024px) {
  .login-container {
    flex-direction: column;
    width: 500px;
    height: auto;
  }
  
  .info-panel {
    padding: 40px 35px 20px 35px;
  }
  
  .features-section {
    display: none;
  }
  
  .login-panel {
    padding: 20px 35px 40px 35px;
  }
}

@media (max-width: 768px) {
  .login-container {
    width: 95%;
    margin: 20px;
    border-radius: 16px;
  }
  
  .info-panel, .login-panel {
    padding: 30px 25px;
  }
  
  .camera-wrapper {
    margin-top: 20px;
    margin-bottom: 20px;
    padding-top: 18px;
  }
  
  /* 移动端登录成功页面优化 */
  .success-icon {
    width: 80px;
    height: 80px;
  }
  
  .success-title {
    font-size: 28px;
  }
  
  .modern-user-card {
    padding: 20px;
  }
  
  .stats-grid {
    grid-template-columns: 1fr;
    gap: 10px;
  }
  
  .success-actions {
    flex-direction: column;
    gap: 10px;
  }
  
  .modern-button {
    width: 100%;
    min-width: auto;
  }
  
  .face-id-value {
    font-size: 14px;
  }
  
  .stat-card {
    padding: 15px;
  }
  
  .stat-icon-wrapper {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }
}
</style>
